#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@author:  runyuanye
@contact: runyuanye@163.com
"""
import os

select_faceid_list_path = r'/rootfs/media/yery/Kaso/data/ErisedPersonGender/select_image_label_merge.txt'
typical_faceid_list_path = r'/rootfs/media/yery/Kaso/data/ErisedPersonGender/typical_file_faceid_list.txt'
file_faceid_list_path = r'/rootfs/media/yery/Kaso/data/ErisedPersonGender/file_faceid_list.txt'
out_faceid_list_path = r'/rootfs/media/yery/Kaso/data/VideoGenderCropPerson/file_faceid_merge_list.txt'


def main():
    file_faceid_list = []
    with open(file_faceid_list_path, 'r') as file:
        for line in file.readlines():
            # gd/zs/sqqjd/yxxhjt/150100414a54443452067fa6d4be6600/video/20190926_161748/00013_000.jpg 2789
            lines = line.strip().split()
            file_name = lines[0]
            faceid = int(lines[1])
            file_faceid_list.append([file_name, faceid])

    select_file_faceids = {}
    with open(select_faceid_list_path, 'r') as file:
        for line in file.readlines():
            # gd/zs/sqqjd/yxxhjt/150100414a54443452067fa6d4be6600/video/20190926_161748/00013_000.jpg 2789
            lines = line.strip().split()
            file_name = lines[0]
            faceid = int(lines[1])
            select_file_faceids[file_name] = faceid

    typical_to_select_faceids = {}
    with open(typical_faceid_list_path, 'r') as file:
        for line in file.readlines():
            # gd/zs/sqqjd/yxxhjt/150100414a54443452067fa6d4be6600/video/20190926_161748/00013_000.jpg 2789
            lines = line.strip().split()
            file_name = lines[0]
            typical_faceid = int(lines[1])
            select_faceid = select_file_faceids.get(file_name, None)
            if select_faceid is None:
                typical_to_select_faceids[typical_faceid] = typical_faceid
            else:
                typical_to_select_faceids[typical_faceid] = select_faceid

    file_faceid_list.sort(key=lambda x: x[0])
    with open(out_faceid_list_path, 'w') as file:
        for file_name, faceid in file_faceid_list:
            merge_faceid = typical_to_select_faceids.get(faceid, None)
            if merge_faceid is None:
                merge_faceid = faceid
            out_info = '{} {}\n'.format(file_name, merge_faceid)
            file.write(out_info)
    os.system('chmod a+wr {}'.format(out_faceid_list_path))
    print('Finish!')


if __name__ == '__main__':
    main()
